package net.jeanhwea.ex1;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class GetPage {
	public static int BUFFER_SIZE = 8192;
	
	public void downloadPage(URL u) throws IOException {
		InputStream is = u.openStream();
		
		StringBuilder res = new StringBuilder();
		byte buffer[] = new byte[BUFFER_SIZE];
		int size = 0;
		
		do{
			size = is.read(buffer);
			if(size != -1){
				res.append(new String(buffer, 0, size));
			}
		}while(size != -1);
		
		System.out.println(res.toString());
		
	}

	public void goToURL(String url){
		try{
		URL u = new URL(url);
		try {
			downloadPage(u);
		} catch (IOException e) {
			e.printStackTrace();
		}
		} catch(MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		String url = "http://www.baidu.com";
		GetPage gp = new GetPage();
		gp.goToURL(url);
	}

}
